
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ScoreCardModel.mixins.serialize_mixin &#8212; ScoreCardModel  documentation</title>
    <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head>
  <body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for ScoreCardModel.mixins.serialize_mixin</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;用于序列化Mixin,使用该Mixin的类可以用方法`obj.dumps()`来生成可以存储的一串字符串</span>
<span class="sd">也可以用类方法`clz.loads(dump_str)`来获取一个该类的实例对象.</span>


<span class="sd">&gt;&gt;&gt; obj.dumps()</span>
<span class="sd">&gt;&gt;&gt; clz.loads(dump_str)</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">dill</span>
<span class="kn">import</span> <span class="nn">base64</span>


<div class="viewcode-block" id="SerializeMixin"><a class="viewcode-back" href="../../../ScoreCardModel.mixins.html#ScoreCardModel.mixins.serialize_mixin.SerializeMixin">[docs]</a><span class="k">class</span> <span class="nc">SerializeMixin</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;序列化模型Mixin</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="SerializeMixin.dumps"><a class="viewcode-back" href="../../../ScoreCardModel.mixins.html#ScoreCardModel.mixins.serialize_mixin.SerializeMixin.dumps">[docs]</a>    <span class="k">def</span> <span class="nf">dumps</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;序列化模型</span>

<span class="sd">        Returns:</span>

<span class="sd">            str: - 序列化后的字符串,使用`dill`将对象序列化为bytes,再用`base64`将bytes转化为可以传输,可以存储的字符串.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">dill</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span></div>

<div class="viewcode-block" id="SerializeMixin.loads"><a class="viewcode-back" href="../../../ScoreCardModel.mixins.html#ScoreCardModel.mixins.serialize_mixin.SerializeMixin.loads">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">loads</span><span class="p">(</span><span class="n">clz</span><span class="p">,</span> <span class="n">dump_str</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;反序列化模型</span>

<span class="sd">        Parameters:</span>

<span class="sd">            dump_str (str): - 序列化后的字符串</span>

<span class="sd">        Returns:</span>

<span class="sd">            clz: - 反序列化后返回该类型的实例对象</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dill</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">dump_str</span><span class="p">))</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper"><div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2017, 87.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.5</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
      
    </div>

    

    
  </body>
</html>